/* eslint-disable no-use-before-define */
import {
  getQueryString, trimText, doCalculation, doPost, sendSbList, reportSaveData_ydDo
} from './tool.js';


const handleQysds = async () => {
  let lis = $('#main-left > div:nth-child(2) > ul > li');
  let A201010 = Array.from(lis).find((item) => $(item).text().includes('A201010'));
  let A201020 = Array.from(lis).find((item) => $(item).text().includes('A201020'));
  let A200000 = Array.from(lis).find((item) => $(item).text().includes('A200000'));
  // let isSave = await handleA201010(A201010);
  // let isSave1 = await handleA201010(A201020);
  let isSave2 = await handleA201010(A200000);
};
const handleA201010 = async (btn) => {
  return new Promise((resolve, rejcet) => {
    $(btn).find('a').click();
    let dataUrl = $(btn).find('a').attr('data-url');
    let timer = setInterval(async () => {
      let iframes = $('#tab-content > div > iframe');
      let iframe = Array.from(iframes).find((item) => $(item).attr('src') === dataUrl);
      if (iframe) {
        clearInterval(timer);
        timer = null;
        console.log(iframe);
        let result = await optionTable(iframe);
        // resolve(result);
      }
    }, 1000);
  });
};

const optionTable = async (iframe) => {
  return new Promise(async (resolve, rejcet) => {
    let win = iframe.contentWindow;
    if ($(iframe).attr('src') === '/ReportJdAction_2019.do?activity=getData&reportname=SB_JD_A_2015') {
      // win.querySelector('#tab > tbody > tr:nth-child(31) > td.noleft > input[name=jsrgdynssx]:nth-child(2)').click();
      if ($(iframe).contents().find('.layui-layer-iframe').length) {
        let box = $(iframe).contents().find('.layui-layer-iframe');
        box.find('.layui-layer-btn0')[0].click();
      }
      $(iframe).contents().find('#tab > tbody td.noleft > input[name=jsrgdynssx]:nth-child(2)')[0].click();
      $(iframe).contents().find('#tab > tbody td.noleft > input[name=gjxzhjzhy]:nth-child(2)')[0].click();
      $(iframe).contents().find('#tab > tbody td.noleft > input[name=kjxzxqy]:nth-child(2)')[0].click();
      $(iframe).contents().find('#tab #C23_1')[0].innerText = 1;
      $(iframe).contents().find('#tab #C24_1')[0].innerText = 1;
      // $(iframe).contents().find('#tab #C25_1')[0].innerText = 0.01;
      // $(iframe).contents().find('#tab #C26_1')[0].innerText = 0.01;
      $(iframe).contents().find('#saveTg')[0].click();
      win.reportSaveData_ydDo = reportSaveData_ydDo.bind(win);
      let result = await closeBox(iframe);
    } else if ($(iframe).attr('src') === '/ReportJdGdjszjAction_2018.do?activity=getData&reportname=SB_JD_GDZCZJ') {
      $(iframe).contents().find('#saveTg')[0].click();
    } else {
      win.doCalculation = doCalculation.bind(win);
      win.doPost = doPost.bind(win);
      win.loadxx();
      win.save();
    }
    let timer1 = setInterval(() => {
      let resultInfo = $(iframe).contents().find('.layui-layer-dialog > div.layui-layer-content');
      if (resultInfo.length) {
        clearInterval(timer1);
        timer1 = null;
        if (resultInfo.text().includes('保存成功')) {
          $(iframe).contents().find('.layui-layer-dialog > div.layui-layer-btn.layui-layer-btn- > a')[0].click();
          resolve(true);
        } else {
          resolve(false);
        }
      }
    }, 200);
  });
};

const closeBox = (iframe) => {
  return new Promise(async (resolve, rejcet) => {
    let timer = setInterval(() => {
      let resultInfo = $(iframe).contents().find('.layui-layer-dialog > div.layui-layer-content');
      if (resultInfo.length) {
        clearInterval(timer);
        timer = null;
        if (resultInfo.text().includes('季初资产总额、季末资产总额”按“万元”单位填报！！！')) {
          $(iframe).contents().find('.layui-layer-dialog > div.layui-layer-btn.layui-layer-btn- > a')[0].click();
          resolve(true);
        } else {
          resolve(false);
        }
      }
    }, 200);
  });
};

export { handleQysds };
